package com.atguigu.tingshu.user.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.common.rabbit.service.RabbitService;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;
	// 启动时自动从spring 容器中获取
	//现需要重新定义当前对象，不需要使用启动,需要自己重新
	@Autowired
	private WxMaService wxMaService;

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private RabbitService rabbitService;



	@Override
	public void updateUser(UserInfoVo userInfoVo, Long userId) {
		//通过用户id获取数据库信息
		UserInfo userInfo = this.getById(userId);
		userInfo.setNickname(userInfoVo.getNickname());
		userInfo.setAvatarUrl(userInfoVo.getAvatarUrl());
		//修改数据
		userInfoMapper.updateById(userInfo);
	}

	@Override
	public UserInfoVo getUserInfo(Long userId) {
		//创建对象
		UserInfoVo userInfoVo = new UserInfoVo();
		// 根据id查询用户信息
		UserInfo userInfo = this.getById(userId);
		// 封装数据
		BeanUtils.copyProperties(userInfo, userInfoVo);
		return userInfoVo;
	}

	@Override
	public Map<String, Object> wxLogin(String code) {
		// 调用服务方法
		WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
		try {
			wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(code);
		} catch (WxErrorException e) {
			throw new RuntimeException(e);
		}
		// 获取openid
		String openid = wxMaJscode2SessionResult.getOpenid();
		//使用 openid 查询数据库，判断用户是否已经注册
		UserInfo userInfo = this.getOne(new LambdaUpdateWrapper<UserInfo>().eq(UserInfo::getWxOpenId, openid));
		// 判断用户是否已经注册
		if (null == userInfo) {
			// 用户没有注册过
			userInfo = new UserInfo();
			userInfo.setWxOpenId(openid);
			userInfo.setNickname("听友："+System.currentTimeMillis());
			userInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
			// 保存用户信息
			this.save(userInfo);
			// 注册就送钱！ 1000块; OpenFeignClient  发送信息
			rabbitService.sendMessage(MqConst.EXCHANGE_USER, MqConst.ROUTING_USER_REGISTER, userInfo.getId());


		}
		// 直接返回登录成功
		String token = UUID.randomUUID().toString().replace("-", "");
		//从缓存中获取数据
		String userLoginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
		this.redisTemplate.opsForValue().set(userLoginKey, userInfo, RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
		// 创建 Map 集合封装数据
		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		//返回数据
		return map;
	}
}
